package com.itheima.handler;

import com.itheima.common.entity.Result;
import com.itheima.common.exception.BaseException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(BaseException ex){
        // 捕获自定义异常BaseException(否则要用Exception)
        return new Result(false, ex.getMessage());
    }
}
